package com.cci.kangdao.fieldService.controller;

import com.cci.kangdao.fieldService.aop.model.ChannelThreadLocal;
import com.cci.kangdao.aop.RequestBodyThreadLocal;
import com.cci.kangdao.fieldService.dto.request.CCGRequest;
import com.cci.kangdao.fieldService.dto.request.SyncCCGEvaluationRequest;
import com.cci.kangdao.fieldService.dto.request.SyncFieldServiceEngineerRequest;
import com.cci.kangdao.fieldService.dto.request.SyncFieldServiceItemRequest;
import com.cci.kangdao.fieldService.dto.request.SyncFieldServiceTemplateRequest;
import com.cci.kangdao.fieldService.dto.request.SyncFieldServiceTemplateStandardRequest;
import com.cci.kangdao.fieldService.dto.request.SyncFieldServiceWorkOrderRequest;
import com.cci.kangdao.fieldService.dto.request.SyncWorkStatusRequest;
import com.cci.kangdao.fieldService.model.FieldServiceEngineer;
import com.cci.kangdao.fieldService.model.FieldServiceEvaluation;
import com.cci.kangdao.fieldService.model.FieldServiceItem;
import com.cci.kangdao.fieldService.model.SyncFieldServiceOrderStatus;
import com.cci.kangdao.fieldService.model.FieldServiceTemplate;
import com.cci.kangdao.fieldService.model.FieldServiceTemplateStandard;
import com.cci.kangdao.fieldService.model.FieldServiceWorkOrder;
import com.cci.kangdao.fieldService.service.CCGSyncService;
import com.cci.kangdao.utilTool.BeanUtils;
import com.cci.kangdao.utilTool.JsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author William Du
 * @date 2019/12/30
 */
@Controller
public class CCGSyncController {

    private static final Logger logger = Logger.getLogger(CCGSyncController.class);

    @Resource
    private CCGSyncService ccgSyncService;

    @RequestMapping(value = "syncCcgServiceItem.aspx", method = RequestMethod.POST)
    public void syncCcgServiceItem() {
        CCGRequest<SyncFieldServiceItemRequest> request = getRequest(SyncFieldServiceItemRequest.class);
        FieldServiceItem item = BeanUtils.convert(request.getParams(), FieldServiceItem.class);
        item.setChannel(ChannelThreadLocal.get());
        ccgSyncService.syncServiceItemFromCrm(item);
    }

    @RequestMapping(value = "syncCcgTemplate.aspx", method = RequestMethod.POST)
    public void syncCcgTemplate() {
        CCGRequest<SyncFieldServiceTemplateRequest> request = getRequest(SyncFieldServiceTemplateRequest.class);
        FieldServiceTemplate template = BeanUtils.convert(request.getParams(), FieldServiceTemplate.class);
        template.setChannel(ChannelThreadLocal.get());
        ccgSyncService.syncServiceTemplateFromCrm(template);
    }

    @RequestMapping(value = "syncCcgStandard.aspx", method = RequestMethod.POST)
    public void syncCcgStandard() {

        CCGRequest<List<SyncFieldServiceTemplateStandardRequest>> request = getListRequest(SyncFieldServiceTemplateStandardRequest.class);
        List<FieldServiceTemplateStandard> standardList = BeanUtils.convertList(request.getParams(), FieldServiceTemplateStandard.class);
        ccgSyncService.syncTemplateStandardFromCrm(ChannelThreadLocal.get(), standardList);
    }

    @RequestMapping(value = "syncCcgEngineers.aspx", method = RequestMethod.POST)
    public void syncCcgEngineers() {
        CCGRequest<List<SyncFieldServiceEngineerRequest>> request = getListRequest(SyncFieldServiceEngineerRequest.class);
        List<FieldServiceEngineer> engineerList = BeanUtils.convertList(request.getParams(), FieldServiceEngineer.class);
        ccgSyncService.syncEngineerFromCrm(ChannelThreadLocal.get(), engineerList);
    }

    @RequestMapping(value = "syncCcgWorkOrderInfo.aspx", method = RequestMethod.POST)
    public void syncCcgServiceWorkOrderInfo() {
        CCGRequest<SyncFieldServiceWorkOrderRequest> request = getRequest(SyncFieldServiceWorkOrderRequest.class);
        FieldServiceWorkOrder workOrder = BeanUtils.convert(request.getParams(), FieldServiceWorkOrder.class);
        workOrder.setChannel(ChannelThreadLocal.get());
        ccgSyncService.syncWorkOrderFromCrm(workOrder);
    }

    @RequestMapping(value = "syncCCGWorkOrderStatus.aspx", method = RequestMethod.POST)
    public void syncCCGWorkOrderStatus() {
        CCGRequest<SyncWorkStatusRequest> request = getRequest(SyncWorkStatusRequest.class);
        SyncFieldServiceOrderStatus orderStatus = BeanUtils.convert(request.getParams(), SyncFieldServiceOrderStatus.class);
        orderStatus.setChannel(ChannelThreadLocal.get());
        ccgSyncService.syncOrderStatusFromCrm(orderStatus);
    }

    @RequestMapping(value = "syncCCGEvaluationInfo.aspx", method = RequestMethod.POST)
    public void syncCCGEvaluationInfo() {
        CCGRequest<SyncCCGEvaluationRequest> request = getRequest(SyncCCGEvaluationRequest.class);
        FieldServiceEvaluation evaluation = BeanUtils.convert(request.getParams(), FieldServiceEvaluation.class);
        evaluation.setChannel(ChannelThreadLocal.get());
        ccgSyncService.syncOrderEvaluation(evaluation);
    }

    @SuppressWarnings("unchecked")
    private <T> CCGRequest<T> getRequest(Class<T> tClass) {
        String requestBody = RequestBodyThreadLocal.get();
        CCGRequest<T> request = JsonUtils.toJavaObject(requestBody, CCGRequest.class);
        String paramJSON = JsonUtils.pickSub(requestBody, "params");
        request.setParams(JsonUtils.toJavaObject(paramJSON, tClass));
        return request;
    }

    @SuppressWarnings("unchecked")
    private <T> CCGRequest<List<T>> getListRequest(Class<T> tClass) {
        String requestBody = RequestBodyThreadLocal.get();
        CCGRequest<List<T>> request = JsonUtils.toJavaObject(requestBody, CCGRequest.class);
        String paramJSON = JsonUtils.pickSub(requestBody, "params");
        request.setParams(JsonUtils.convertJsonArrayToList(paramJSON, tClass));
        return request;
    }

}
